<?
$savedata = extractBooleanWithKeyFromArray("savedata", $_POST, 0);
if ($savedata) {
	
	$dateadded = extractStringWithKeyFromArray("dateadded", $_POST, date("Y-m-d H:i:s"));
	$datemodified = extractStringWithKeyFromArray("datemodified", $_POST, date("Y-m-d H:i:s"));
	$name = magicClean(extractStringWithKeyFromArray("name", $_POST, null));
	$description = magicClean(extractStringWithKeyFromArray("description", $_POST, null));
	$id = extractNumberWithKeyFromArray("id", $_POST, null);
	
	if ($id) {
		$query = "UPDATE smslist_list SET name='".$name."', description='".$description."', 
				datemodified='".$datemodified."' WHERE id='".$id."'";
		$db->executeQuery($query);
	}
	else {
		$query = "INSERT INTO smslist_list (name, description, active, dateadded, datemodified)
				VALUES ('".$name."', '".$description."', 1, '".$dateadded."', '".$datemodified."')";
		$db->executeQuery($query);
		$id = mysql_insert_id();
	}
	
	$query = "DELETE FROM smslist_cms_listusers WHERE listid='".$id."'";
	$db->executeQuery($query);
	
	$listusers = extractArrayWithKeyFromArray("adminusers", $_POST, array());
	foreach ($listusers as $user) {
		$query = "INSERT INTO smslist_cms_listusers (listid, uid) VALUES ('".$id."', '".$user."')";
		$db->executeQuery($query);
	}
	
	$query = "DELETE FROM smslist_cms_listgroups WHERE listid='".$id."'";
	$db->executeQuery($query);
	
	$listgroups = extractArrayWithKeyFromArray("admingroups", $_POST, array());
	foreach ($listgroups as $group) {
		$query = "INSERT INTO smslist_cms_listgroups (listid, gid) VALUES ('".$id."', '".$group."')";
		$db->executeQuery($query);
	}
	
	redirectCleanly("/cms/html/index.php?fileNo=1&formid=".SMS_LIST_FORM."&sorts=clear&direction=clear");
}

$id = extractNumberWithKeyFromArray("id", $_REQUEST, 0);

if ($id) {
	$smsListTableWriter = new CmsTableWriter("smslist_list", $db);
	$smsListTableWriter->resetCustomIterator(" WHERE id='".$id."'");
	if ($row = $smsListTableWriter->nextItem()) {
		$dateadded = date("Y-m-d H:i:s", strtotime($row->dateadded));
		$datemodified = date("Y-m-d H:i:s");
		$name = magicClean($row->name);
		$description = magicClean($row->description, false);
	}
	else {
		die("Invalid id!");
	}
}
else {
	$dateadded = date("Y-m-d H:i:s");
	$datemodified = date("Y-m-d H:i:s");
	$name = "";
	$description = "";
}
?>

<div class="cms_browse_heading">
  <div class="cms_browse_heading_label">
    <h1>Add SMS List</h1>
  </div>
  <div class="cms_browse_addrecord"></div>
</div>
<div id="cms_browse_topdivider" class="cms_browse_divider"></div>
<form class="cms_form" action="/cms/html/index.php" method="post" enctype="multipart/form-data" name="addMailListUserForm" id="addMailListUserForm">
  <input type="hidden" name="savedata" value="1">
  <?
  if (isset($id) && $id) {
	  ?>
	  <input type="hidden" name="id" value="<?= $id; ?>">
	  <?
  }
  ?>
  <div class="cms_masteredit">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="cms_masteraddedit_table" >
      <tr>
        <td height="1" width="160"><img src="/cms/images/spacer.gif" height="1" width="160" /></td>
	<td height="1"><img src="/cms/images/spacer.gif" height="1" width="1" /></td>
      </tr>
      <tr class="cms_masteraddedit_table"> 
        <td class="cms_form_label" >Date Added:</td> 
	<td class="cms_form_element" align="left">
	  <span class="cms_form_textlabel"><?= date("l d F Y H:i:s", strtotime($dateadded)); ?></span>
	  <input type="hidden" name="dateadded" value="<?= date("Y-m-d H:i:s", strtotime($dateadded)); ?>">
	</td>

      </tr>
      <tr class="cms_masteraddedit_table"> 
        <td class="cms_form_label" >Last Updated:</td> 
	<td class="cms_form_element" align="left">
	  <span class="cms_form_textlabel"><?= date("l d F Y H:i:s", strtotime($datemodified)); ?></span>
	  <input type="hidden" name="modified" value="<?= date("Y-m-d H:i:s", strtotime($dateadded)); ?>">
	</td>
      </tr>
      <tr class="cms_masteraddedit_table"> 
        <td class="cms_form_label" >Name:</td> 
	<td class="cms_form_element" align="left">

	  <input type="text" name="name" value="<?= $name; ?>" class="cms_form_text"></input>
	</td>
      </tr>
      <tr class="cms_masteraddedit_table"> 
        <td class="cms_form_label" >Description:</td> 
	<td class="cms_form_element" align="left">
	  <textarea name="description" class="cms_form_textarea"><?= $description; ?></textarea>
	</td>
      </tr>
	  <tr class="cms_masteraddedit_table">
	    <td class="cms_form_label">Administrative Users:</td>
	    <td class="cms_form_element" align="left">
		  <?
		  $checked = array();
		  $query = "SELECT uid, username FROM user ORDER BY username";
		  if (isset($id) && $id) {
			  $db->executeQuery("SELECT uid FROM smslist_cms_listusers WHERE listid='".$id."'");
			  while ($checkRow = $db->nextRow()) {
				  $checked[] = $checkRow[0];
			  }
		  }
		  else {
			  $checked[] = $authInfo->uid;
		  }
		  selectBoxList("adminusers", $query, $db, $checked, "addMailListUserForm", processAttribs(array("id" => "adminusers", "class" => "cms_form_dualselect"), array()));
		  ?>
	    </td>
	  </tr>
	  <tr class="cms_masteraddedit_table">
	    <td class="cms_form_label">Administrative Groups:</td>
	    <td class="cms_form_element" align="left">
		  <?
		  $checked = array();
		  $query = "SELECT gid, groupname FROM grouplist ORDER BY groupname";
		  if (isset($id) && $id) {
			  $db->executeQuery("SELECT gid FROM smslist_cms_listgroups WHERE listid='".$id."'");
			  while ($checkRow = $db->nextRow()) {
				  $checked[] = $checkRow[0];
			  }
		  }
		  selectBoxList("admingroups", $query, $db, $checked, "addMailListUserForm", processAttribs(array("id" => "admingroups", "class" => "cms_form_dualselect"), array()));
		  ?>
	    </td>
	  </tr>
      <tr>
        <td colspan="2" align="right" valign="middle">
	  <script language="javascript" type="text/javascript"> function selectAllElementsInSelect( select ) { for (i=0; i < select.options.length; i++){ select.options[i].selected = true; } return; } function doOnSubmitForElementsOnForm( formName ){ f = document.getElementById( formName ); if (f != null){ for (j = 0; j < f.length; j++ ){ e = f[j]; if (e.onsubmit){ e.onsubmit(); } } } return; } </script>
	  <a class="cms_form_submit" href="javascript:doOnSubmitForElementsOnForm('addMailListUserForm');document.forms['addMailListUserForm'].submit();" ><img src="/cms/images/button_go.gif" /></a>
	</td>
      </tr>
    </table>

  </form>
</div>